package com.xx.questionnaire.dao.realize;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.xx.questionnaire.dao.UserDaoI;
import com.xx.questionnaire.util.dao.Dbcp_connection;
import com.xx.questionnaire.util.dao.JDBCUtil;
import com.xx.questionnaire.util.dao.JdbcI;
import com.xx.questionnaire.util.datetime.GetDateTime;
import com.xx.questionnaire.util.datetime.GetDateTimeI;

/**
 * 只操作 USER table
 * 
 * @author Administrator
 * 
 */
public final class UserDao implements UserDaoI {
	static Logger log = Logger.getLogger(UserDao.class);
	private JDBCUtil jdbcu = null;
	private ResultSet rs = null;
	private JdbcI jdbc=null;
	public PreparedStatement ps = null;
	private List<String> userList;

	/**
	 * 以传入的参数为条件查询数据库。
	 * 
	 * @return List<String>
	 */
	public List<String> getUserTable(String username, String password) {
		String sql = "SELECT * from user where user_name=? AND password= ? ";
		jdbcu = new JDBCUtil();
		ps = jdbcu.getPS(sql);

		try {
			ps.setString(1, username);
			ps.setString(2, password);
			rs = ps.executeQuery();
			userList = new ArrayList<String>();
			while (rs.next()) {
				userList.add(rs.getString(2));
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		jdbcu.close(rs);

		return userList;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		UserDaoI userd = new UserDao();
		List<String> al = userd.getUserTable("wangzhihua", "123");
		for (String a : al) {
			System.out.println(a);
		}
		System.out.println("sdf");
		userd.getUserID("wangzhihua");
	}

	public int getUserID(String username) {
		 
		String SQL = "SELECT id FROM user where user_name= ? ";
		Dbcp_connection dbcp = new Dbcp_connection();

		ps = dbcp.getPStatement(SQL);

		try {
			ps.setString(1, username);
			rs = ps.executeQuery();
			System.out.println(rs.getFetchSize());
			log.info(Thread.currentThread().getStackTrace()[1].getMethodName()+"查询结果："+rs.getFetchSize());
			 return rs.getFetchSize();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
			dbcp.close(ps, rs);
			
			
		}
		return -1;
 
	}

	public int addUser(String username, String password, String email,
			String website, int age, String description) {
			GetDateTimeI dt = new GetDateTime();
		 jdbc = new Dbcp_connection();
		  String sqlInsert = "insert into user(user_name,nice,password,birthday,phone,email) values(?,?,?,?,?,?)";	
			
			ps = jdbcu.getPS(sqlInsert);
			try {
				ps.setString(1, username);
				ps.setString(2,username);
				ps.setString(3, password);
				ps.setString(4,dt.getDateTime());
				ps.setString(5, "");
				ps.setString(6, "");
			int count = ps.executeUpdate();
			return count;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				jdbc.close(ps);
			}
		return 0;
	}

}
